<!--在线用户-->
<template>
  <div id="proof" class="app-container">
    <div class="filter-container" style="padding-bottom:10px">
      共<span class="color-primary">{{ list.length }}</span>位在线用户
    </div>
    <!--列表-->
    <div class="table-container flex-main">
      <el-table
        ref="multipleTable"
        :data="list"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        @sort-change="sortChange">
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="会话编号" prop="sessionId" min-width="240" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <copy-text :content="scope.row.sessionId" position="left" />
          </template>
        </el-table-column>
        <el-table-column label="用户名" prop="userName" width="140" />
        <el-table-column label="部门公司" prop="orgName" :show-overflow-tooltip="true" min-width="160">
          <template slot-scope="scope">
            {{ scope.row.orgName }}
          </template>
        </el-table-column>
        <el-table-column label="登录IP" prop="ip" align="center" width="130" />
        <el-table-column label="登录地址" prop="loginAddress" align="center" width="130" />
        <el-table-column label="浏览器" prop="browser" align="center" width="180" />
        <el-table-column label="操作系统" prop="os" align="center" width="140" />
        <el-table-column prop="gmtCreated" sortable align="center" label=" 登录时间" width="150">
          <template slot-scope="scope">{{ scope.row.loginTime | formatDate }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="90" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <button-option icon="el-icon-switch-button" skin="danger" title="强制退出" @click="handleForceLogout(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        name: undefined,
        sort: this.common.SORT_DEFAULT
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 表格自带方法开始
    /**
     * 表格列排序
     */
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    async fetchData() {
      const response = await this.$store.dispatch('userOnlinePage', this.listQuery)
      this.list = response.records
      this.list.filter((item) => {
        item.isDisabled = !item.isDisabled
        return true
      })
      this.total = response.total
    },

    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleForceLogout(row) {
      this.$confirm('您确定强制退出用户[' + row.userName + '], 是否继续?', '强退提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('userForceLogout', row.sessionId).then(response => {
          this.handleFilter()
          this.msgSuccess('强退成功!')
        }).catch(() => {})
      }).catch(() => { })
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
